package com.example.emos.wx.controller;

import com.example.emos.wx.common.util.R;
import com.example.emos.wx.controller.form.TestSayHelloForm;
import com.example.emos.wx.db.pojo.TbUserEntity;
import com.example.emos.wx.service.TbUserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/test")
@Tag(name = "测试Web接口")
public class TestController {

    private static final Logger logger = LoggerFactory.getLogger(TestController.class.getName());


    @Autowired
    private TbUserService tbUserService;

    @Autowired
    private ObjectMapper objectMapper;

    @PostMapping("/sayHello")
    @Operation(summary = "最简单的测试方法")
    public R sayHello(@Valid @RequestBody TestSayHelloForm form) {
        return R.ok().put("message", "Hello,"+form.getName());
    }

    /**
     * 保存用户信息
     *
     * @param user 用户对象
     * @return 保存成功的用户对象
     */
    @PostMapping("/saveUser")
    @Operation(summary = "保存用户信息")
    public R saveUser(@RequestBody TbUserEntity user) throws JsonProcessingException {
        user.setCreateTime(new Date());
        boolean save = tbUserService.save(user);
        if (save) {
            logger.info("保存用户成功:{}", user.toString());
        }
        return R.ok().put("data", user);
    }

    /**
     * 查询用户信息
     *
     * @param id 用户ID
     * @return 查询结果
     */
    @GetMapping("getUser")
    @Operation(summary = "查询用户信息")
    public R getUser(Integer id) {
        TbUserEntity user = tbUserService.getById(id);
        logger.info("查询结果:{}", user.toString());
        return R.ok().put("data", user);
    }
}


